<?php

/**
 * Program form.
 *
 * @package    sf_sandbox
 * @subpackage form
 * @author     Your name here
 * @version    SVN: $Id: sfDoctrineFormTemplate.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class ProgramForm extends BaseProgramForm {

    public function configure() {
        sfProjectConfiguration::getActive()->loadHelpers(array('Url'));
        unset($this['is_approval_sent']);
        $this->widgetSchema['is_mandatory'] = new sfWidgetFormInputCheckbox();

        $this->widgetSchema['start_date'] = new sfHijreeDateWidget();
        $this->widgetSchema['end_date'] = new sfHijreeDateWidget();

        $timing_type_arr = ProgramTable::getInstance()->getTimingTypes();
        $this->widgetSchema['training_timing_type'] = new sfWidgetFormChoice(array('choices' => $timing_type_arr));
        $this->validatorSchema['training_timing_type'] = new sfValidatorChoice(array('choices' => array_keys($timing_type_arr)));

        $this->widgetSchema['trainer_id'] = new sfWidgetChoiceWithAdd(array('add_url' => 'trainer/newPopup',
                    'model' => 'Trainer',
                    'add_empty' => true,
                    'window_height' => 400
                ));

        $this->widgetSchema['created_at'] = new sfWidgetFormInputText(array(), array('readonly' => 'readonly'));
        $this->widgetSchema['updated_at'] = new sfWidgetFormInputText(array(), array('readonly' => 'readonly'));

        $this->widgetSchema['code_target_list'] = new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'CodeTarget', 'expanded' => true, 'renderer_class' => 'sfCheckboxWithallWidget'));
        $this->widgetSchema['code_sector_list'] = new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'CodeSector', 'expanded' => true, 'renderer_class' => 'sfCheckboxWithallWidget'));

        $levels_opts = LinkProgramLevelTable::getInstance()->getLevels();
        $this->widgetSchema['level_list'] = new sfWidgetFormChoice(array('multiple' => true, 'choices' => $levels_opts, 'expanded' => true));
        $this->validatorSchema['level_list'] = new sfValidatorChoice(array('multiple' => true, 'choices' => array_keys($levels_opts)));

        if (!$this->isNew()) {
            $this->getWidget('level_list')->setDefault($this->getObject()->getLevelIds());
        }

        $this->getWidget('teacher_list')->setOption('renderer_class', 'sfWidgetFormSelectDoubleList');

        $this->widgetSchema['location_of_holding'] = new sfWidgetChoiceWithOther(array('choices' => array(), 'specify_other_text' => 'Specify location'));
        $this->validatorSchema['location_of_holding'] = new sfValidatorString(array('required' => true));

        $this->widgetSchema['created_by_user_id'] = new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Creator'), 'add_empty' => true), array('disabled' => 'disabled'));
        $this->widgetSchema['updated_by_user_id'] = new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('LastModifier'), 'add_empty' => true), array('disabled' => 'disabled'));

        $this->mergePostValidator(new sfValidatorSchemaCompare('start_date', sfValidatorSchemaCompare::LESS_THAN_EQUAL, 'end_date', array(), array('invalid' => 'START_END_DATE_RANGE_INVALID')));

        $this->widgetSchema->setLabels(array(
            'name' => 'Program name',
            'program_code' => 'Program code',
            'Trainer' => 'Trainer',
            'is_mandatory' => 'Mandatory?',
            'training_timing_type' => 'Training Time',
            'duration_days' => 'Program duration in days',
            'duration_hours' => 'Program duration in hours',
            'start_date' => 'Training start date',
            'end_date' => 'Training end date',
            'location_of_holding' => 'Location held',
            'target_number' => 'Target participant number',
            'updated_at' => 'Last update date',
            'created_at' => 'Creation date',
            'code_target_list' => 'Target categories',
            'level_list' => 'Level'
        ));

        $this->widgetSchema->setHelps(array(
            'target_number' => 'TARGET_NUMBER_HELP_TIP',
            'notes' => 'PROGRAM_NOTES_HELP_TIP'
        ));

        if ($this->isNew()) {
            unset($this['created_at'], $this['updated_at']);
        }

        unset($this['teacher_list']);
        unset($this['academic_year_id']);

        /* ------------- */

        $user = sfContext::getInstance()->getUser();

        if ($user->hasAttribute('program_type') && $user->getAttribute('program_type') == 'night') {
            unset($this['is_mandatory']);
            unset($this['program_type']);
        }

        if ($user->hasAttribute('program_type') && $user->getAttribute('program_type') == 'day') {
            unset($this['course_id']);
        }
        
        /* ------------- */

        unset($this['training_timing_type']);

        $this->setCustomFormatter(); // to display form with stars
    }

    public function doSave($con = null) {
        parent::doSave($con);
        $this->saveLevels();
    }

    public function save($con = null) {
        $user = sfContext::getInstance()->getUser();
        $obj = parent::save($con);
        $obj = $this->getObject();

        if ($user->hasAttribute('kts_global_program_filter') && is_array($data = $user->getAttribute('kts_global_program_filter'))) {
            $obj->set('semester', $data['semester']);
            $obj->set('academic_year_id', $data['year']);
        }

        /* --------------- */

        if ($user->hasAttribute('program_type') && $user->getAttribute('program_type') == 'night') {
            $obj->set('program_type', 'night');
            $obj->set('global_program_type', 'night');
            $obj->set('is_mandatory', false);
        }
        
        if ($user->hasAttribute('program_type') && $user->getAttribute('program_type') == 'day') {
            $obj->set('global_program_type', 'day');
        }


        /* --------------- */


        $obj->save();
        return $obj;
    }

    protected function saveLevels() {
        $q = Doctrine_Query::create()->from('LinkProgramLevel')
                        ->addWhere('program_id = ?', $this->object->getId())
                        ->delete()->execute();

        foreach ($this->getValue('level_list') as $level) {
            $link = new LinkProgramLevel();
            $link->setProgramId($this->object->getId());
            $link->setLevelId($level);
            $link->save();
        }
    }

}
